﻿//---------------------------------------------------------------------
// Author: Reinhard Lehrbaum
//
// Description: Base class for commands which require DirectoryEntry
//              and works with Active Directory.
//
// Creation Date: 2006-12-30
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.DirectoryServices;
using System.Management.Automation;

namespace Pscx.Commands.Internal
{
    public abstract class PscxDirectoryServiceCommandBase : PscxCmdlet
    {
        protected string FServer;
        protected PSCredential FCredentials;


        [Alias("DC")]
        [Parameter(HelpMessage = "Send the request to this active directory domain controller.")]
        public string Server
        {
            get { return FServer; }
            set { FServer = value; }
        }

        [Parameter(HelpMessage = "Specifies credentials required to authenticate on the domain controller."),
         Credential,
         ValidateNotNull]
        public PSCredential Credential
        {
            get { return FCredentials; }
            set { FCredentials = value; }
        }


        protected DirectoryEntry GetDirectoryEntry(string Path)
        {
            DirectoryEntry result = new DirectoryEntry(Path);
            if (FCredentials != null)
            {
                result.Username = FCredentials.UserName;
                result.Password = FCredentials.GetNetworkCredential().Password;
            }
            return result;
        }
    }
}
